package com.ycky.connect.websocket;

import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @description: websocket服务端
 * @author: cjj
 * @time: 2025/3/18 16:53
 */
@Data
@Slf4j
@Component
@ServerEndpoint("/WebSocket/{uid}")
public class WebSocketServer {
    private Session session;
    private String uid;

    @OnOpen
    public void onOpen(Session session, @PathParam("uid") String uid) {
        this.session = session;
        this.uid = uid;
        WebSocketManager.addWebSocketServer(this);
        log.info("用户【{}】连接成功", uid);
    }

    @OnClose
    public void onClose() {
        WebSocketManager.removeWebSocketServer(this);
        log.info("用户【{}】断开连接", uid);
    }

    @OnMessage
    public void onMessage(String message, Session session) {

    }

    @OnError
    public void onError(Session session) {
        log.error("连接出错，错误消息：{}", uid);
    }
}